/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: jse2204 - T15
 * <p>Powered by webrx On 2022-10-09 09:56:45
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T15 {
    Object obj = new Object();

    void work() {
        String t = Thread.currentThread().getName();
        System.out.println(t + ":线程启动...");

        if ("t3".equalsIgnoreCase(t)) {
            try{
                TimeUnit.SECONDS.sleep(5);
            }catch(Exception e){
                e.printStackTrace();
            }
            synchronized (obj) {
                //obj.notify();
                obj.notifyAll();
            }
        } else {
            for (int i = 0; i < 3; i++) {
                System.out.printf("%s = %d%n", t, i);
                synchronized (obj) {
                    try {
                        if (i < 1) {
                            System.out.println(t + "wait()....");
                            obj.wait(); //没有唤醒   notify()
                        }
                        //TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        //wait() sleep() notify() notifyAll()

        T15 t = new T15();
        new Thread(t::work, "t1").start();
        new Thread(t::work, "t2").start();
        new Thread(t::work, "t3").start();


    }
}
